package service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import common.dao.AdminMapper;
import common.pojo.SysAdmin;
import common.utils.Constants;



@Service
public class AdminServiceImpl {
	/**
	 * 引用dao层
	 */
	@Autowired
	private AdminMapper adminMapper;

	public SysAdmin getByName(String name) {
		return adminMapper.getByName(name);
	}
	
	public SysAdmin findById(int id) {
		return adminMapper.queryById(id);
	}

	public boolean update(SysAdmin admin) {
		adminMapper.updateBySelective(admin);
		return true;
	}

	// public boolean updateAdmin(SysAdmin admin) {
	// adminMapper.updateAdmin(admin);
	// return true;
	// }
	//
	public boolean updatepwd(SysAdmin admin) {

		adminMapper.update(admin);
		return true;
	}

	public List<SysAdmin> find(int start, int limit) {
		return adminMapper.queryListByWhere("where deleted="
				+ Constants.NOT_DELETED + " order by create_time desc limit "
				+ start + "," + limit);
	}

	public int queryByCount(SysAdmin sysAdmin) {
		return adminMapper.queryByCount(sysAdmin);
	}

	public boolean saveAdmin(SysAdmin admin) {
		admin.setDeleted(Constants.NOT_DELETED);
		admin.setState(Constants.SHOW);
		adminMapper.add(admin);
		return true;
	}

	public boolean delete(String ids, String name) {
		String[] id = StringUtils.split(ids, ",");
		SysAdmin ad = new SysAdmin();
		for (int i = 0; i < id.length; i++) {
			if (StringUtils.isNotBlank(id[i])) {
				ad.setDeleted(Constants.DELETED);
				ad.setUpdateName(name);
				ad.setUpdateTime(new Date());
				ad.setId(Integer.valueOf(id[i]));
				adminMapper.updateBySelective(ad);
			}
		}
		return true;
	}

	

}
